package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.query.BpmnInfoQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * 客户控制器
 */
@Controller
@RequestMapping("/business/bpmnInfo")
public class BpmnInfoController {
    //模板前缀
    private static final String prefix = "/business/bpmnInfo/";

    @Autowired
    private IBpmnInfoService bpmnInfoService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("system:bpmnInfo:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }
    //部署的页面
    @RequestMapping("/deployPage")
    public String deployPage(){
        return prefix+"deploy";
    }


    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("system:bpmnInfo:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<BpmnInfo> query(BpmnInfoQuery qo){
        return bpmnInfoService.query(qo);
    }
    //删除
    @RequiresPermissions("system:bpmnInfo:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        bpmnInfoService.deleteBatch(ids);
        return AjaxResult.success();
    }
    @RequestMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(MultipartFile bpmnFile,String bpmnType,String bpmnLabel,String info) throws IOException {
        bpmnInfoService.deploy(bpmnFile,bpmnType,bpmnLabel,info);
        return AjaxResult.success();
    }
    @RequestMapping("/readResource")
    @ResponseBody
    public void readResource(HttpServletResponse response,Long id,String type) throws IOException {
        InputStream is = bpmnInfoService.getInputStreamByType(id,type);
        IOUtils.copy(is,response.getOutputStream());
    }
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        bpmnInfoService.delete(id);
        return AjaxResult.success();
    }
}
